package cn.hsa.fqz.task.controller;

import cn.hsa.fqz.common.enums.FileType;
import cn.hsa.fqz.handler.base.BaseController;
import cn.hsa.fqz.task.controller.vo.TaskFeedbackVO;
import cn.hsa.fqz.task.controller.vo.TaskRoleAuthVO;
import cn.hsa.fqz.task.entity.TTask;
import cn.hsa.fqz.task.entity.TTaskFeedback;
import cn.hsa.fqz.task.entity.TTaskProcess;
import cn.hsa.fqz.task.enums.UserCategoryEnums;
import cn.hsa.fqz.task.service.ITaskFeedbackService;
import cn.hsa.fqz.task.service.TSysFileAccessoryService;
import cn.hsa.fqz.task.service.TTaskMedcClueFeedbackService;
import cn.hsa.fqz.task.service.TTaskService;
import cn.hsa.fqz.task.service.dto.*;
import cn.hsa.fqz.util.UserRegionUtils;
import cn.hsa.hsaf.core.framework.context.HsafContextHolder;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *
 */
@Slf4j
@Api(value = "任务管理", tags = "任务管理")
@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {


    @Autowired
    private TTaskService tTaskService;

    @Autowired
    private ITaskFeedbackService taskFeedbackService;

    @Autowired
    private TSysFileAccessoryService sysFileAccessoryService;

    @Autowired
    private TTaskMedcClueFeedbackService tTaskMedcClueFeedbackService;

    @Value("${zmjk.ck.host}")
    private String ckHost;

    @Value("${zmjk.ck.port}")
    private Integer ckPort;

    @Value("${zmjk.ck.passwd}")
    private String ckPasswd;

    @ApiOperation(value = "发布任务", notes = "发布任务")
    @GetMapping("/publish")
    public WrapperResponse<Boolean> publish(@RequestParam Integer csId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.addTask(loginUser, csId);
    }

    @ApiOperation(value = "任务列表", notes = "任务列表")
    @PostMapping("/list")
    public WrapperResponse<PageResult<TTask>> list(@RequestBody TaskQueryDTO query) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        query.getCondition().setBelongTo(enums.toString());
        query.getCondition().setSupplierStatus(1);
        if(enums != UserCategoryEnums.GLOBAL){
            query.getCondition().setPrv(loginUser.getRegion());
        }
        return tTaskService.getPageByCondition(query);
    }

    @ApiOperation(value = "行刑衔接任务列表", notes = "行刑衔接任务列表")
    @PostMapping("/ga/list")
    public WrapperResponse<PageResult<TTask>> galist(@RequestBody TaskQueryDTO query) {
        //TODO 用户权限去掉  根据行政区划查询数据
        return tTaskService.getPageByCondition(query);

    }

    @ApiOperation(value = "行刑衔接分配", notes = "行刑衔接分配")
    @PostMapping("/role/auth")
    public WrapperResponse<Integer> authTaskRole(@RequestBody TaskRoleAuthVO vo){
        return tTaskService.updateAuthTaskRole(vo);
    }

    @ApiOperation(value = "执行任务", notes = "执行任务")
    @PostMapping("/execute")
    public WrapperResponse<Boolean> execute(
                                       @RequestBody TaskExecDTO taskExec
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.execute(loginUser, taskExec);
    }

    @ApiOperation(value = "任务栈", notes = "任务栈")
    @PostMapping("/stack")
    public WrapperResponse<PageResult<TTask>> stack(
                                               @RequestBody TaskQueryDTO query) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        query.getCondition().setBelongTo(enums.name());
        query.getCondition().setSupplierStatus(0);
        return tTaskService.getPageStackTaskByCondition(query);
    }


    @ApiOperation(value = "执行任务回调", notes = "执行任务回调")
    @GetMapping("/execute/callback")
    public WrapperResponse<Boolean> executeCallback(@RequestParam Integer taskId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.executeCallback(loginUser, taskId);
    }

    @ApiOperation(value = "发布子任务", notes = "发布子任务")
    @GetMapping("/publishSub")
    public WrapperResponse<Boolean> publishSub(@RequestParam Integer taskId,
                                          @RequestParam String... orgId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return  tTaskService.addSubTask(loginUser, taskId, orgId);
    }

    @ApiOperation(value = "获取当前用户的父任务", notes = "获取当前用户的父任务")
    @GetMapping("/root/tasks")
    public WrapperResponse<List<TTask>> getRootTaskByBelongTo() {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO dto = getLoginUser(currentUser);
        List<TTask> result = tTaskService.getRootTaskByBelongTo(dto.getRegion());
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "任务签收列表", notes = "任务签收列表")
    @PostMapping("/sign/list")
    public WrapperResponse<PageResult<TTask>> signList(@RequestBody TaskQueryDTO query) {
        //当前用户数据权限
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        query.getCondition().setRegion(loginUser.getRegion());
        query.getCondition().setSupplierStatus(1);
        return tTaskService.getPageSubTaskByCondition(query);
    }


    @ApiOperation(value = "待分发列表审核", notes = "待分发列表审核")
    @PostMapping("/supplier/list")
    public WrapperResponse<PageResult<TTask>> supplierList(@RequestBody TaskQueryDTO query) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        query.getCondition().setBelongTo(enums.toString());
        query.getCondition().setSupplierStatus(0);
        if(enums != UserCategoryEnums.GLOBAL){
            query.getCondition().setPrv(loginUser.getRegion());
        }
        return tTaskService.getPageByCondition(query);
    }

    @ApiOperation(value = "审核确认待分发", notes = "审核确认分发")
    @PostMapping("/supplier")
    public WrapperResponse<Boolean> supplier(@RequestParam("taskId")Integer taskId,@RequestParam("supplierStatus")Integer supplierStatus) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.addSupplier(taskId,supplierStatus);
    }

    @ApiOperation(value = "可用任务列表", notes = "可用任务列表")
    @GetMapping("/enabled/list")
    public WrapperResponse<List<TTask>> enableList() {
        //当前用户数据权限
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        return tTaskService.enableList(enums,loginUser.getRegion());
    }

    @ApiOperation(value = "任务分发", notes = "任务分发")
    @GetMapping("/distribute")
    public WrapperResponse<Boolean> distribute(@RequestParam Integer taskId,
                                          @RequestParam String region
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return  tTaskService.addDistribute(loginUser, taskId, region);
    }

    @ApiOperation(value = "一键分发", notes = "分发")
    @GetMapping("/distribute/all")
    public WrapperResponse<Boolean> distributeAll(@RequestParam Integer pTaskId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return  tTaskService.addDistributeAll(loginUser, pTaskId);
    }

    @ApiOperation(value = "撤回分发", notes = "撤回分发")
    @GetMapping("/rollback")
    public WrapperResponse<Boolean> rollback(@RequestParam Integer taskId,
                                               @RequestParam String region) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return  tTaskService.addRollback(loginUser, taskId);
    }

    @ApiOperation(value = "查询反馈状态", notes = "查询反馈状态")
    @PostMapping("/feedback/status")
    public WrapperResponse<List<MedcFeedbackMinDTO>> getMinDtoByTaskId(@RequestBody TaskFeedbackVO vo){
        List<MedcFeedbackMinDTO> result = tTaskMedcClueFeedbackService.genMinDtosByTaskIdAndMedcCode(vo.getTaskId(), vo.getMedicCode());
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "任务取回", notes = "任务取回")
    @GetMapping("/back")
    public WrapperResponse<Boolean> back(@RequestParam Integer taskId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.addBack(loginUser, taskId);
    }

    @ApiOperation(value = "任务办结", notes = "任务办结")
    @GetMapping("/finish")
    public WrapperResponse<Boolean> finish(@RequestParam("taskId") Integer taskId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.addFinish(loginUser, taskId);
    }

    @ApiOperation(value = "申请办结", notes = "申请办结")
    @GetMapping("/supplier/finish")
    public WrapperResponse<Boolean> supplierFinish(@RequestParam("taskId") Integer taskId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.addSupplierFinish(loginUser, taskId);
    }

    @ApiOperation(value = "任务签收", notes = "任务签收")
    @GetMapping("/receive")
    public WrapperResponse<Boolean> receive(@RequestParam Integer taskId,@RequestParam(required = false) Integer needSplit
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.addReceive(loginUser, taskId,needSplit);
    }

    @ApiOperation(value = "任务移交", notes = "任务移交")
    @GetMapping("/sendTo")
    public WrapperResponse<Integer> sendTo(@RequestParam Integer taskId) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.addSendTo(loginUser, taskId);
    }

    @ApiOperation(value = "详情查看", notes = "详情查看")
    @GetMapping("/detail")
    public WrapperResponse<TaskDetailDTO> detail(@RequestParam Integer taskId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        //String region = loginUser.getRegion();
        return tTaskService.getTaskDetail(taskId);
    }

    @ApiOperation(value = "查询任务可分发行政区划", notes = "查询任务可分发行政区划")
    @GetMapping("/getEnableProvinceList")
    public WrapperResponse<List<RegionDTO>> getEnableProvinceList(@RequestParam Integer taskId
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return tTaskService.getEnableProvinceList(ckHost,ckPort,ckPasswd,loginUser.getRegion(),taskId);
    }

    @ApiOperation(value = "分发流程", notes = "分发流程")
    @PostMapping("/flow/list")
    public WrapperResponse<PageResult<TTaskProcess>> flow(@RequestBody TaskFlowQueryDTO query) {
        return tTaskService.getFlowPageByCondition(query);
    }

    @ApiOperation(value = "子任务添加反馈", notes = "子任务添加反馈")
    @PostMapping("/addFeedback")
    public WrapperResponse<Boolean> addFeedback(@RequestBody AddTaskFeedbackDTO create) {

        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();

        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return WrapperResponse.success(taskFeedbackService.add(loginUser,create));
    }

    @ApiOperation(value = "反馈查询", notes = "反馈查询")
    @PostMapping("/feedback/list")
    public WrapperResponse<List<TTaskFeedback>> feedbackList(@RequestParam Integer taskId) {
        return WrapperResponse.success(taskFeedbackService.list(taskId));
    }

    @ApiOperation(value = "反馈详情", notes = "反馈详情")
    @PostMapping("/feedback/detail")
    public WrapperResponse<TTaskFeedback> feedbackDetail(@RequestParam Integer feedbackId) {
        return WrapperResponse.success(taskFeedbackService.detail(feedbackId));
    }

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/file/upload")
    public WrapperResponse<String> uploadFile(@RequestParam String clueId,
                                               @RequestParam MultipartFile file) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO dto = super.getLoginUser(currentUser);
        return WrapperResponse.success(sysFileAccessoryService.upload(clueId, FileType.TK,dto, file));
    }
}
